﻿
Pages_Help = new function () {

    this.helpID = 0;
    this.type = -1;
    this.title = '';
    this.gameId = 1;
    this.lstGame = new Array();
    this.m_objPageHelp = new VtcPaging("divHelpPaging", "Pages_Help.bindData", "pagingCss", 10, CONSTANT.PAGE_DISPLAY);
    this.documentReady = function (m_lstGames) {
        this.lstGame = m_lstGames;
        this.searchListHelp();
    };
    this.getGameInfo = function (id) {
        for (var i = 0; i < this.lstGame.length; i++) {
            if (this.lstGame[i].Id == id)
                return this.lstGame[i];
        }
        return null;
    };
    this.bindData = function (_cur,_ps) {
        Loading.showProcess();
        $.ajax({
            type: "GET",
            url: "/handler/HandlerContent.ashx",
            data: { type: this.type, title: this.title, t: "helpGetList",currentPage:_cur, pageSize:_ps },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                Pages_Help.processData(_cur,data);

                Loading.closeProcess();
            }
        });

    };

    this.processData = function (_cur, data) {
        $("#spTotalHelp").html('(' + data.TotalRecord.toString() + ')');
        $("#divListHelp").setTemplateURL("/Template/Pages/ListHelp.htm");
        $("#divListHelp").processTemplate(data);
        Pages_Help.setStyleRow("#divListHelp");
        Pages_Help.setRowNumber("#divListHelp");
        Pages_Help.m_objPageHelp.bindPaging(_cur, data.TotalRecord);
        if (data.TotalRecord <= Pages_Help.m_objPageHelp.pageSize) {
            $("#divHelpPaging").attr('style', 'display:none');
        }
        else {
            $("#divHelpPaging").attr('style', 'display:block');
        }
    };

    this.setRowNumber = function (m_selParent) {
        var m_stt = 0;
        $('#divSTT', m_selParent).each(function () {
            m_stt++;
            $(this).text(m_stt);
        });
    };

    this.searchListHelp = function () {
        this.gameId = $("#ddlGame").val();
        this.type = $("#ddlType").val();
        this.title = $.trim($('#txtTitle').val());
        this.bindData(1, this.m_objPageHelp.pageSize);
    };

    $("#btnSearch").live('click', function () {
        Pages_Help.searchListHelp();
    });

    $("#liEdit").live('click', function () {
        $('.context-menu').hide();
        Pages_Help.displayControl(false);
        Pages_Help.helpID = $(this.parentNode).attr("id").substring(2);
        Pages_Help.typeButton = 1;
        $.ajax({
            type: "GET",
            url: "/handler/HandlerContent.ashx",
            data: { id: Pages_Help.helpID, t: "helpGetInfo" },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                $("#divCreateDate").attr('style', 'display:block');
                $("#lbCreateDate").text(COMMON.jSonDateToString(data.ModifiedDate));
                tinyMCE.activeEditor.setContent(data.Content);
                $("#ddlTypeUpdate").val(data.Type);
                $("#ddlStatusUpdate").val(data.Status);
                $("#txtDescription").val(data.Description);
                $("#ddlGameUpdate").val(data.HiLoGameId);
                $("#txtTitleUpdate").val(data.Title);
            }
        });

    });

    $("#btnAddHelp").live('click', function () {
        Pages_Help.displayControl(false);
        Pages_Help.helpID = -1;
        $("#divCreateDate").attr('style', 'display:none');
        tinyMCE.activeEditor.setContent("");
    });

    $("#btnUpdateHelp").live('click', function () {
        if (tinyMCE.activeEditor.getContent().replace('<p>', '').replace('</p>', '') == '') {
            jAlert(MESSAGE.errorInfo, MESSAGE.title);
            return;
        }
        var title = $.trim($("#txtTitleUpdate").val());
        if (title == '') {
            jAlert(MESSAGE.errorInfo, MESSAGE.title);
            return;
        }
        var _data =
                {
                    gameId: $("#ddlGameUpdate").val(),
                    id: Pages_Help.helpID,
                    type: $("#ddlTypeUpdate").val(),
                    title: title,
                    description: $.trim($("#txtDescription").val()),
                    content: tinyMCE.activeEditor.getContent().replace('<p>', '').replace('</p>', ''),
                    status: $("#ddlStatusUpdate").val()
                };
        Loading.showProcess();
        $.ajax({
            type: "POST",
            url: "/WebService/WSContent.asmx/HelpUpdate",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                Loading.closeProcess();
                if (data.d > 0) {
                    jAlert(MESSAGE.updateSuccess, MESSAGE.title);
                    Pages_Help.type = -1;
                    Pages_Help.helpID = -1;
                    Pages_Help.bindData();
                    Pages_Help.displayControl(true);
                }
                else {
                    if (data.d == -1) {
                        jAlert(MESSAGE.notPermission, MESSAGE.title);
                    }
                    else {
                        jAlert(MESSAGE.error, MESSAGE.title);
                    }
                }
            }
        });
    });

    $("#liDelete").live('click', function () {
        Pages_Help.helpID = $(this.parentNode).attr("id").substring(2);
        jConfirm("Bạn có chắc chắn xóa bản ghi này không?", MESSAGE.confirm, function (result) {
            if (result) {
                var _data = {
                    id: Pages_Help.helpID
                };
                Loading.showProcess();
                $.ajax({
                    type: "POST",
                    url: "/WebService/WSContent.asmx/HelpDelete",
                    data: JSON.encode(_data),
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    cache: false,
                    success: function (data) {
                        Loading.closeProcess();
                        Pages_Help.helpID = -1;
                        if (data.d > 0) {
                            jAlert(MESSAGE.deleteSuccess, MESSAGE.title);
                            Pages_Help.bindData();
                        }
                        else {
                            if (data.d == -1) {
                                jAlert(MESSAGE.notPermission, MESSAGE.title);
                            }
                            else {
                                jAlert(MESSAGE.error, MESSAGE.title);
                            }
                        }
                    }
                });
            }
        });
    });

    $("#liLock").live('click', function () {
        Pages_Help.helpID = $(this.parentNode).attr("id").substring(2);
        var _data = {
            id: Pages_Help.helpID,
            status: 0
        };
        Loading.showProcess();
        $.ajax({
            type: "POST",
            url: "/WebService/WSContent.asmx/HelpUpdateStatus",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                Loading.closeProcess();
                if (data.d > 0) {
                    jAlert(MESSAGE.updateSuccess, MESSAGE.title);
                    Pages_Help.bindData();
                }
                else {
                    if (data.d == -1) {
                        jAlert(MESSAGE.notPermission, MESSAGE.title);
                    }
                    else {
                        jAlert(MESSAGE.error, MESSAGE.title);
                    }
                }
            }
        });
    });

    $("#liUnlock").live('click', function () {
        Pages_Help.helpID = $(this.parentNode).attr("id").substring(2);
        var _data = {
            id: Pages_Help.helpID,
            status: 1
        };
        Loading.showProcess();
        $.ajax({
            type: "POST",
            url: "/WebService/WSContent.asmx/HelpUpdateStatus",
            data: JSON.encode(_data),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            cache: false,
            success: function (data) {
                Loading.closeProcess();
                if (data.d > 0) {
                    jAlert(MESSAGE.updateSuccess, MESSAGE.title);
                    Pages_Help.bindData();
                }
                else {
                    if (data.d == -1) {
                        jAlert(MESSAGE.notPermission, MESSAGE.title);
                    }
                    else {
                        jAlert(MESSAGE.error, MESSAGE.title);
                    }
                }
            }
        });
    });


    $("#btnCancelHelp").live('click', function () {
        Pages_Help.displayControl(true);
    });

    $("#aContextMenu").live('click', function () {
        $('.context-menu').hide();
        $(this).next().toggle();
    });

    $(document).click(function (event) {
        if ($('.context-menu').is(':visible') && !$(event.target).closest(".context-menu").size() && !$(event.target).closest("#aContextMenu").size()) {
            $('.context-menu').hide();
        }
    });

    // Ẩn hiện các khối Tìm kiếm, danh sách và thông tin
    this.displayControl = function (m_bool) {
        if (m_bool) {
            $("#divHelpDetail").slideUp(300, function () {
                $("#divSearch").slideDown(300, function () {
                    $("#divList").slideDown(300);
                });

            });
        }
        else {
            $("#divList").slideUp(300, function () {
                $("#divSearch").slideUp(300, function () {
                    $("#divHelpDetail").slideDown(300);
                });
            });
        }
    };

    this.setStyleRow = function (m_selParent) {
        var m_stt = 0;
        $('#divItem', m_selParent).each(function () {
            m_stt++;
            if ((m_stt % 2) == 0) {
                $(this.parentNode).addClass('even');
            }
            else {
                $(this.parentNode).addClass('odd');
            }
        });
    };
}    